/*
 * Copyright (c) 2021. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

import com.xxpure.Point.MyPoint;
import com.xxpure.Point.PointFactory;
import com.xxpure.compare.KMeansCompare;
import com.xxpure.kmeans.KMeans;

import java.util.ArrayList;

/**
 * @program: KNN
 * @author: Mr.xiao
 * @email: 1347772729@qq.com
 * @create: 2021-07-04 18:49
 **/
public class BinarySearchK {

    private static final KMeansCompare compare = new KMeansCompare();
    static ArrayList<MyPoint> initialData = new PointFactory(1000, 10000).getInitialData();

    private static boolean check(int k) {
        KMeans<MyPoint> means = new KMeans<>(initialData, k);
        ArrayList<MyPoint> results;
        while (true) {
            try {

                means.nextSample(compare);

            } catch (Exception e) {
                break;
            }
        }
        results = means.getSamples();
        if (results == null)
            return true;
        for (var result : results) {
            if (result == null)
                return true;
        }
        return false;
    }

    public static int binarySearch(int l, int r) {
        while (l < r) {
            int mid = l + r >> 1;
            if (check(mid)) r = mid;
            else l = mid + 1;
        }
        return l;

    }

    public static void main(String[] args) {
        System.out.println(binarySearch(1, 1000));
    }
}
